let express = require("express");
const { json } = require("express/lib/response");
let router = express.Router();
let createDBconnect = require("../../db/createDBconnect");

router.get("/energy/forest", (req, res) => {
  try {
    let connect = createDBconnect();
    // console.log(req.query);
    let sqlString = `select * from forest_area where \`地区\` = '${req.query.cityName}' union select * from forest_coverage where \`地区\`='${req.query.cityName}'`;
    connect.query(sqlString, (err, result) => {
      if (err) {
        console.log(`SQL error: ${err}!`);
        res.status(401).send("数据库查询出错");
      }

  
      result = JSON.parse(JSON.stringify(result));
      let resultArr = []; //返回的数据需要的类型是数组
      for(let item in result[0])
      {
          resultArr.unshift({
              year: item,
              area: result[0][item],
              coverage: result[1][item],
          })
      }
      resultArr.pop();
    //   console.log(result);
    // console.log(resultArr)
      res.send(resultArr);
    });
  } catch (err) {
    console.log(err);
  }
});

module.exports = router;
